package cn.wangzg.course0603;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import cn.wangzg.course0603.adapter.CommonAdapter;
import cn.wangzg.course0603.adapter.ViewHolder;
import cn.wangzg.course0603.bean.Article;
import cn.wangzg.course0603.util.HttpCallbackModelListener;
import cn.wangzg.course0603.util.HttpUrlConnectionUtils;
import cn.wangzg.course0603.view.XListView;

public class MainActivity2 extends AppCompatActivity {
    private XListView listArticle;
    private View footer;

    private CommonAdapter<Article.DataBean.DatasBean> adapter;
    private int curPage=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        initView();
        initData();
    }

    private void initData() {
        HttpUrlConnectionUtils.doGet(this,
                "https://www.wanandroid.com/article/list/0/json",
                new HttpCallbackModelListener() {
                    @Override
                    public void onFinish(Object response) {
                        adapter.setmDatas(((Article) response).getData().getDatas());
                        curPage++;
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                }, Article.class);
    }

    private void initView() {
        //handler = new MyHandler(this);
        footer = getLayoutInflater().inflate(R.layout.footer, null);
        listArticle = findViewById(R.id.listArticle);

        listArticle.setPullLoadEnable(true);//允许加载更多
        listArticle.setPullRefreshEnable(true);//允许下拉刷新
        listArticle.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {//重新加载第一页
                initData();
                curPage=1;
                listArticle.stopRefresh();//加载完成，隐藏头部视图
            }

            @Override
            public void onLoadMore() {
                HttpUrlConnectionUtils.doGet(MainActivity2.this,
                        "https://www.wanandroid.com/article/list/"+curPage+"/json",
                        new HttpCallbackModelListener() {
                            @Override
                            public void onFinish(Object response) {
                                adapter.addmDatas(((Article) response).getData().getDatas());
                                curPage++;
                                listArticle.stopLoadMore();//加载完成，隐藏底部视图
                            }

                            @Override
                            public void onError(Exception e) {

                            }
                        }, Article.class);
            }
        });
        //使用通用适配器，建议大家掌握，但是考试时不考此内容
        adapter = new CommonAdapter<Article.DataBean.DatasBean>(this, null,
                R.layout.item_article) {
            @Override
            public void convert(ViewHolder helper, final Article.DataBean.DatasBean item) {
                helper.setText(R.id.textView, item.getTitle());
                helper.setText(R.id.textView3, item.getShareUser());
                helper.setText(R.id.textView5, item.getSuperChapterName()
                        + "/" + item.getChapterName());
                helper.setText(R.id.textView7, item.getNiceDate());
                helper.getView(R.id.imageView).setSelected(item.isCollect());
                helper.getView(R.id.imageView).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        v.setSelected(!v.isSelected());
                        item.setCollect(v.isSelected());
                    }
                });
            }
        };
        listArticle.setAdapter(adapter);
        //添加单击事件


    }
}
